package org.gvp.generator.core.writer.impl;

import org.gvp.generator.core.WriterToResources;
import org.gvp.generator.core.writer.AbstractFileWriter;
import org.gvp.generator.props.CreateProps;
import org.gvp.generator.props.ProjectProps;
import org.gvp.generator.table.TableInfo;

import java.nio.file.Path;
import java.nio.file.Paths;

@WriterToResources
public class TypescriptFileWriter extends AbstractFileWriter {
    @Override
    public void execute(TableInfo tableInfo, ProjectProps project) {
        String comm = "/** %s数据类型声明 */".formatted(tableInfo.getRemark());
        String typeDefine = "export type %s = {".formatted(tableInfo.getUpperName());
        channel.writeNewline(comm);
        channel.writeNewline(typeDefine);
        tableInfo.getColumns().forEach(e -> {
            String content = """
                        /** %s */
                        %s: %s;
                    """.formatted(e.getRemark(),e.getLowerName(),e.getTypescriptType());
            channel.write(content);
        });
        channel.writeNewline("}");
        channel.writeNewline("export default %sColumns;".formatted(tableInfo.getLowerName()));
    }

    @Override
    public boolean isWriterFile(CreateProps create) {
        return create.isCreateTypescript();
    }

    @Override
    public Path getFilePath(String basePath, ProjectProps project, String fileName) {
        return Paths.get(basePath,project.getTypescriptDirPath(),fileName + ".d.ts");
    }
}
